﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.IO;
using System.Drawing.Imaging;
using System.Net;
using CarLoanSystem.Web.Comman;
using CarLoanSystem.DAL;
using CarLoanSystem.Entity;

namespace CarLoanSystemWeb.Page.Register
{
    public partial class wuc_register_member : UserControlManageForm
    {
        public event EventHandler InfoMessege;
        public event EventHandler ErrorMessege;
        public event EventHandler ErrorMessegeName;
        public event EventHandler InfoMessegeUserName;
        public event EventHandler ErrorMessegeUserName;
        private static bool checkUsername;
        private static string username;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtName.Focus();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                CustomerItem item = new CustomerItem();
                item.CUSTOMER_ADDRESS = this.txtAddress.Text;
                item.CUSTOMER_AGE = Convert.ToInt32(this.txtAge.Text);
                item.CUSTOMER_JOB = this.txtJob.Text;
                item.CUSTOMER_LASTNAME = this.txtLastName.Text;
                item.CUSTOMER_NAME = this.txtName.Text;
                item.CUSTOMER_PERSONAL_ID = this.txtPerId.Text;
                //item.CUSTOMER_SARARY = Convert.ToInt32(this.txtSalary.Text);
                item.CUSTOMER_TEL = this.txtTel.Text;
                item.CUSTOMER_WORK_ADDRESS = this.txtAddressWork.Text;
                item.CUSTOMER_WORK_TEL = this.txtTelWork.Text;
                item.CUSTOMER_BLACK_LIST = "N";
                item.USERNAME = this.txtUsername.Text;
                item.PASSWORD = this.txtPassword.Text;
                if (checkUsername && username.Equals(this.txtUsername.Text))
                {
                    CustomerInfoDAL.Insert(item);
                    if (InfoMessege != null)
                    {
                        InfoMessege(this,EventArgs.Empty);
                    }
                }
                else 
                {
                    if (ErrorMessegeUserName != null) 
                    {
                        ErrorMessegeUserName(this,EventArgs.Empty);
                    }
                    this.txtUsername.Focus();
                }
            }
            catch(Exception ex)
            {
                if (ErrorMessege != null)
                {
                    ErrorMessege(this, EventArgs.Empty);
                }
            }
        }

        protected void btnCheckUsername_Click(object sender, EventArgs e)
        {

            if (!CustomerInfoDAL.CheckUsername(this.txtUsername.Text))
            {
                username = this.txtUsername.Text;
                checkUsername = true;
                if (InfoMessege != null)
                {
                    InfoMessegeUserName(this, EventArgs.Empty);
                }
            }
            else 
            {
                if (ErrorMessegeName != null)
                {
                    ErrorMessegeName(this, EventArgs.Empty);
                }
                this.txtUsername.Focus();
                checkUsername = false;
            }
        }
    }
}